import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.JList;
import javax.swing.JToggleButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;

public class VueRetrait extends Vue {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	//private static final long serialVersionUID = 5069349224425178639L;
	private JPanel contentPane;
	private JTextField textFieldNom;
	private DroitsMandataire droitsMand;
	private JButton btnEnregistrer;
	private JButton btnEnregistrer2;
	private ComptePerso _compteP;
	private CompteSociete _compteS;
	private JSeparator separator;
	private JTextField montant;
	private JLabel lblTypeCompte;
	private JLabel typeCompte;
	private JLabel lblSolde;
	private JLabel solde;
	private int type; 
	private JLabel lblType;
	private JComboBox typeMouv;
	private JComboBox listMa;
	private JLabel lblMandataire;
	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public VueRetrait(Controleur controleur) throws ParseException {
		super (controleur);
		setTitle("Effectuer un Dépot");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 600, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNom = new JLabel("Numéro Compte :");
		lblNom.setBounds(38, 30, 120, 16);
		contentPane.add(lblNom);
		
		textFieldNom = new JTextField();
		textFieldNom.setBounds(176, 25, 231, 28);
		textFieldNom.setBackground(new Color(255, 255, 255));
		contentPane.add(textFieldNom);
		textFieldNom.setColumns(10);
		
		btnEnregistrer = new JButton("Rechercher");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String numS = textFieldNom.getText().trim().toLowerCase();
				getControleur().rechCompteRetrait(numS);
			}
		});
		btnEnregistrer.setBounds(454, 24, 117, 29);
		contentPane.add(btnEnregistrer);
		
		separator = new JSeparator();
		separator.setBounds(12, 65, 573, 3);
		contentPane.add(separator);
		
		
		btnEnregistrer2 = new JButton("Enregistrer");
		btnEnregistrer2.setBounds(300, 266, 107, 28);
		contentPane.add(btnEnregistrer2);
		btnEnregistrer2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String m = montant.getText().trim().toLowerCase();
				Retrait.typeRetrait t = Retrait.typeRetrait.valueOf(typeMouv.getSelectedItem().toString());
				if(type == 1){
					getControleur().retraitPerso(m, _compteP, t);
				}
				else{
					getControleur().retraitSociete(m, _compteS, t, droitsMand);
				}
			}
		});
		

		
		montant = new JTextField();
		montant.setBounds(176, 166, 231, 28);
		contentPane.add(montant);
		montant.setColumns(10);
		
		JLabel lblMontant = new JLabel("Montant :");
		lblMontant.setBounds(38, 172, 61, 15);
		contentPane.add(lblMontant);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueRetrait.this);
			}
		});
		btnAnnuler.setBounds(181, 266, 107, 28);
		contentPane.add(btnAnnuler);
		
		lblTypeCompte = new JLabel("Type Compte :");
		lblTypeCompte.setBounds(38, 112, 98, 15);
		contentPane.add(lblTypeCompte);
		
		typeCompte = new JLabel("");
		typeCompte.setBounds(148, 112, 140, 15);
		contentPane.add(typeCompte);
		
		lblSolde = new JLabel("Solde :");
		lblSolde.setBounds(169, 139, 61, 15);
		contentPane.add(lblSolde);
		
		solde = new JLabel("");
		solde.setBounds(242, 139, 61, 15);
		contentPane.add(solde);
		
		lblType = new JLabel("Type :");
		lblType.setBounds(38, 231, 61, 15);
		contentPane.add(lblType);
		
		typeMouv = new JComboBox();
		typeMouv.setBounds(176, 222, 231, 28);
		contentPane.add(typeMouv);
		typeMouv.addItem(Retrait.typeRetrait.Cb.toString());
		typeMouv.addItem(Retrait.typeRetrait.Virement.toString());
		typeMouv.addItem(Retrait.typeRetrait.Guichet.toString());
		
		lblMandataire = new JLabel("Mandataire :");
		lblMandataire.setBounds(38, 85, 120, 15);
		contentPane.add(lblMandataire);
		
		listMa = new JComboBox();
		listMa.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(_compteS != null){
					setMand(_compteS.getMandataire(listMa.getSelectedItem().toString()));
				}
			}
		});
		listMa.setBounds(176, 80, 231, 24);
		contentPane.add(listMa);
		
	}

	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			btnEnregistrer.setEnabled(true);
			break;
			}
		}
	}
	public CompteSociete getCompte(){
		return _compteS;
	}
	
	public void setMand(DroitsMandataire m){
		droitsMand = m;
	}

	public void affiche(CompteSociete compteSoc){
		listMa.setVisible(true);
		lblMandataire.setVisible(true);
		type = 2;
		typeCompte.setText("Compte Societe");
		solde.setText(compteSoc.getSolde().toString());
		_compteS = compteSoc;
	}
	
	public void affiche(ComptePerso comptePer){
		listMa.setVisible(false);
		lblMandataire.setVisible(false);
		type = 1;
		_compteP = comptePer;
		typeCompte.setText("Compte Perso");
		solde.setText(comptePer.getSolde().toString());
	}
	
	public void alimente(HashMap<String, DroitsMandataire> mand) {
		Iterator<String> i = mand.keySet().iterator();
		
		int nb = listMa.getItemCount();
		for (int y=1; y<nb; y++) {
			listMa.removeItemAt(y);
		}
		
		while(i.hasNext()){
			listMa.addItem(i.next());
		}	
	}
}
